import Num from "@/utils/Num";
export const pluck = (objs, picks) =>
  picks.reduce((res, item) => {
    if (item in objs) {
      res[item] = objs[item];
    }
    return res;
  }, {});
export const bc = (formulas, precise = null) =>
  Num.that().bc(formulas, precise);

// 删除后面的0
export const cleanNumber = (num) => Number(num).toString();

export const isNumber = (val) => /^[0-9]+\.?[0-9]*$/.test(val.toString());

// 禁言input type为number的鼠标滚轮事件
export const stopScroll = (evt) => {
  evt = evt || window.event;
  if (evt.preventDefault) {
    // Firefox
    evt.preventDefault();
    evt.stopPropagation();
  } else {
    // IE
    evt.cancelBubble = true;
    evt.returnValue = false;
  }
  return false;
};

// 使用引入的方法找不到 singleNumber函数   箭头函数没有arguments
function randomNum(minNum, maxNum) {
  switch (arguments.length) {
    case 1:
      return parseInt(Math.random() * minNum + 1, 10);
    case 2:
      return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
    default:
      return 0;
  }
}

const genateNumber = () => {
  const time = new Date();
  const year = time.getFullYear() + "";
  let month = time.getMonth() + 1;
  let date = time.getDate();
  let hours = time.getHours();
  let minute = time.getMinutes();
  let second = time.getSeconds();
  if (month < 10) {
    month = "0" + month;
  }
  if (date < 10) {
    date = "0" + date;
  }
  if (hours < 10) {
    hours = "0" + hours;
  }
  if (minute < 10) {
    minute = "0" + minute;
  }
  if (second < 10) {
    second = "0" + second;
  }
  return year + month + date + hours + minute + second + randomNum(1111, 5555);
};

// 销售单
export const singleNumber = () => {
  return "XS" + genateNumber();
};

export const showToast = (title, icon = "none") => {
  uni.showToast({
    title,
    icon,
  });
};

//超过一万显示W结尾
export const formatNumber = (num) => {
  if (num >= 10000) {
    return (num / 10000).toFixed(2) + "W";
  }
  return num;
};

// 通过高德获取经纬度坐标
export const getLocationByGaode = () => {
  return new Promise((resolve, reject) => {
    uni.request({
      url: "https://apis.map.qq.com/ws/location/v1/ip?key=TGFBZ-KONLR-75DWG-WKVHK-V42HQ-WVF5L",
      success: async (res) => {
        resolve({
          latitude: res.data.result.location.lat,
          longitude: res.data.result.location.lng,
        });
      },
      fail: (error) => {
        reject(error);
      },
    });
  });
};
